package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")

public class DishController {

    @Autowired
    private DishService dishService;


    /**
     * 新增菜品
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "user:dish",key = "#dishDTO.categoryId")
    public Result add(@RequestBody @Validated DishDTO dishDTO){
        log.info("新增菜品 {}",dishDTO);
        dishService.add(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询菜品
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult<DishVO>> page(@Validated DishPageQueryDTO pageQueryDTO){
        log.info("菜分页查询 {}",pageQueryDTO);
        PageResult<DishVO> pageResult=dishService.page(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据分类ID查找菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> queryById(@RequestParam("categoryId") Long categoryId){
        log.info("查询{}号分类的菜品",categoryId);
        List<Dish> dishes=dishService.getDishesByCategoryId(categoryId);
        return Result.success(dishes);
    }

    /**
     * 菜品批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    @CacheEvict(cacheNames = "user:dish",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除:{}",ids);
        dishService.deleteBatches(ids);
        return Result.success();
    }

    /**
     * 查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getByIdWithFlavors(@PathVariable("id")Long id){
        log.info("查询{}号菜品信息",id);
        return Result.success(dishService.getDishByIdWithFlavors(id));
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品信息")
    @PutMapping
    @CacheEvict(cacheNames = "user:dish",allEntries = true)
    public Result edit(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息:{}",dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    /** 修改菜品状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("菜品起售和停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "user:dish",allEntries = true)
    public Result startOrStop(@PathVariable("status") Integer status,@RequestParam("id") Long id){
        log.info("修改 {} 号菜品状态: {}",id,status==1?"起售":"停售");
        dishService.updateStatus(id,status);
        return Result.success();
    }



}
